<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">




      <!-- <el-form-item label="是否报警" prop="alert">
        <el-input
          v-model="queryParams.alert"
          placeholder="请输入是否报警"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->


      <!-- <el-form-item label="EPC" prop="epc">
        <el-input
          v-model="queryParams.epc"
          placeholder="请输入EPC"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->



      <el-form-item label="物品编号" prop="proNo">
        <el-input
          v-model="queryParams.proNo"
          placeholder="请输入物品编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="监控时间" prop="createDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.createDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择监控时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="类型" prop="type">
          <el-radio-group v-model="queryParams.type">
            <el-radio label="">全部</el-radio>
            <el-radio label="1">入库</el-radio>
            <el-radio label="2">出库</el-radio>
          </el-radio-group>
      </el-form-item>




      <!-- <el-form-item label="设备编码" prop="sn">
        <el-input
          v-model="queryParams.sn"
          placeholder="请输入设备编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->


      <!-- <el-form-item label="链接ID" prop="connId">
        <el-input
          v-model="queryParams.connId"
          placeholder="请输入链接ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="danger" icon="el-icon-circle-close" size="mini" @click="closeWarningClick">关闭警报</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="detectLogList" border stripe>
      <el-table-column type="index" width="55" align="center" fixed="left"/>
      <!-- <el-table-column label="主键ID" align="center" prop="id" v-if="true"/> -->
      <el-table-column label="物品编号" align="left" prop="proNo" min-width="180" fixed="left"/>
      <el-table-column label="物品名称" align="left" prop="proName" min-width="120" fixed="left"/>
      <el-table-column label="库区/库位" align="left" prop="locator" min-width="260" >
      <template slot-scope="scope">
          {{scope.row.area ? scope.row.area+"/" :""}}
          {{scope.row.groupName ? scope.row.groupName+"/" :""}}
          {{scope.row.cabinetName ? scope.row.cabinetName+"/" :""}}
          {{scope.row.locator ? scope.row.locator :""}}
        </template>
      </el-table-column>
      <el-table-column label="标签码" align="left" prop="qrcode" min-width="160"/>
      <!-- <el-table-column label="EPC" align="center" prop="epc" /> -->
      <!-- <el-table-column label="GPI触发数据" align="center" prop="gpiInfo" /> -->
      <el-table-column label="是否报警" align="center" prop="alert">
      <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.alert == 0">否</el-tag>
          <el-tag type="danger" v-if="scope.row.alert == 1">是</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="天线ID" align="center" prop="tid" /> -->
      <el-table-column label="监控时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type" fixed="right">
        <template slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.type == 1">入库</el-tag>
          <el-tag type="danger" v-if="scope.row.type == 2">出库</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="链接ID" align="center" prop="connId" /> -->
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['buss:detectLog:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['buss:detectLog:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listDetectLog, getDetectLog, delDetectLog, addDetectLog, updateDetectLog } from "@/api/oms/detectLog";
import {getOnlineDevices,closeWarning} from "@/utils/warning"
export default {
  name: "DetectLog",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 门禁监控记录表格数据
      detectLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        alert: undefined,
        epc: undefined,
        proNo: undefined,
        createDate: undefined,
        type: "",
        sn: undefined,
        connId: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询门禁监控记录列表 */
    getList() {
      this.loading = true;
      listDetectLog(this.queryParams).then(response => {
        this.detectLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        gpiInfo: undefined,
        alert: undefined,
        epc: undefined,
        tid: undefined,
        proNo: undefined,
        createDate: undefined,
        type: undefined,
        sn: undefined,
        connId: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加门禁监控记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getDetectLog(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改门禁监控记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateDetectLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addDetectLog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除门禁监控记录编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delDetectLog(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('buss/detectLog/export', {
        ...this.queryParams
      }, `detectLog_${new Date().getTime()}.xlsx`)
    },
    closeWarningClick(){
      this.loading = true;
      getOnlineDevices().then(response => {
        console.info("response::",response)
        if(response && response.data && response.data.data && response.data.data.length > 0){
         let datas = response.data.data;
         for(var i=0;i<datas.length;i++){
           let obj = datas[i];
           // if(obj.onAlert){
             console.info("closeWarning ConnID::",obj.connId)
             closeWarning(obj.connId).then(res => {
             console.info("closeWarning::",res)
             });
           // }
         }
        }
        this.loading = false;
      });
    },

  }
};
</script>
